package com.mycompony.shopping.shangguan.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mycompony.shopping.shangguan.sys.dao.UserAuthMapper;
import com.mycompony.shopping.shangguan.sys.model.Auth;
import com.mycompony.shopping.shangguan.sys.model.User;
import com.mycompony.shopping.shangguan.sys.service.UserAuthService;

public class UserAuthServiceImpl implements UserAuthService {

	private UserAuthMapper userAuthMapper;

	public List<Auth> getUserAuth(User user) {

		if (user == null || StringUtils.isEmpty(user.getUserid())) {
			return null;
		}

		return userAuthMapper.getUserAuth(user);
	}

	public List<Auth> getOrganizationAuth(User user) {
		if (user == null || StringUtils.isEmpty(user.getOrganizationid())) {
			return null;
		}
		return userAuthMapper.getOrganizationAuth(user);
	}

	public UserAuthMapper getUserAuthMapper() {
		return userAuthMapper;
	}

	public void setUserAuthMapper(UserAuthMapper userAuthMapper) {
		this.userAuthMapper = userAuthMapper;
	}

	public Map<String, Auth> getUserAllAuth(User user) {

		Map<String, Auth> map = new HashMap<String, Auth>();

		List<Auth> userAuthList = getUserAuth(user);

		List<Auth> organizationAuthList = getOrganizationAuth(user);

		if (userAuthList != null && !userAuthList.isEmpty()) {
			for (Auth auth : userAuthList) {
				if (auth == null || StringUtils.isEmpty(auth.getAuthid())) {
					break;
				}
				map.put(auth.getAuthid(), auth);
			}
		}

		if (organizationAuthList != null && !organizationAuthList.isEmpty()) {
			for (Auth auth : organizationAuthList) {
				if (auth == null || StringUtils.isEmpty(auth.getAuthid())) {
					break;
				}
				map.put(auth.getAuthid(), auth);
			}
		}

		return map;
	}

}
